//
// qiankun 实例
import {
  registerMicroApps,
  // start,
} from 'qiankun'
import { initActions } from './Action'
import { getRouterInstance } from '@/features/router/router'
import { useTabStore } from '@/features/store/tabStore'
import ConfigInfo from '@/config/config'
import { deviceControl } from '@/utils/electronFunc'

// 获取electron应用用户的设备id
let deviceId = null
// @ts-ignore
deviceControl.deviceIdSend((id: string) => {
  deviceId = id
  // setSubAppsProps({ deviceId: deviceId })
})

// // 设置子应用props的值
// const setSubAppsProps = (obj: { [key: string]: string }) => {
//   devSubApps.forEach((item) => {
//     item.props = { ...item.props, ...obj }
//   })

//   proSubApps.forEach((item) => {
//     item.props = { ...item.props, ...obj }
//   })
// }

// 初始化子应用props
const initSubAppsProps = () => {
  const prop = {
    deviceId: deviceId,
    mainAppRouter: getRouterInstance(),
    onReady: (data) => {
      console.log('子应用已准备就绪', data)
    },
    setAppHistoryPath: (data: { path: string; name: string }) => {
      useTabStore().setAppHistoryPath(data.path, data.name)
    },
  }
  return prop
}

// 子应用
//@ts-ignore
const devSubApps = () => {
  let devSubApps = [
    {
      name: 'sub-resourcePool',
      entry: 'http://localhost:7034',
      container: '#sub-container',
      activeRule: '/subApplication/resourcePool',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-vueComponent',
      entry: 'http://localhost:7035',
      container: '#sub-container',
      activeRule: '/subApplication/vueComponent',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-reactComponent',
      entry: 'http://localhost:7036',
      container: '#sub-container',
      activeRule: '/subApplication/reactComponent',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-aiService',
      entry: 'http://localhost:7038',
      container: '#sub-container',
      activeRule: '/subApplication/aiService',
      props: initSubAppsProps(),
    },
  ]
  return devSubApps
}

//@ts-ignore
const proSubApps = () => {
  let proSubApps = [
    {
      name: 'sub-resourcePool',
      entry: `http://115.190.202.175/resourcePool/?_t=${Date.now()}`,
      container: '#sub-container',
      activeRule: '/subApplication/resourcePool',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-vueComponent',
      entry: `http://115.190.202.175/vueComponent/?_t=${Date.now()}`,
      container: '#sub-container',
      activeRule: '/subApplication/vueComponent',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-reactComponent',
      entry: `http://115.190.202.175/reactComponent/?_t=${Date.now()}`,
      container: '#sub-container',
      activeRule: '/subApplication/reactComponent',
      props: initSubAppsProps(),
    },
    {
      name: 'sub-aiService',
      entry: `http://115.190.202.175/aiService/?_t=${Date.now()}`,
      container: '#sub-container',
      activeRule: '/subApplication/aiService',
      props: initSubAppsProps(),
    },
  ]
  return proSubApps
}

// @ts-ignore
const lifecycle = {
  beforeLoad: [async (app: any) => console.log('before load', app.name)],
  beforeMount: [async (app: any) => console.log('before mount', app.name)],
  afterMount: [async (app: any) => console.log('after mount', app.name)],
  beforeUnmount: [async (app: any) => console.log('before unmount', app.name)],
  afterUnmount: [async (app: any) => console.log('after unmount', app.name)],
}
// // 注册子应用
// if (ConfigInfo.mode === 'dev') registerMicroApps(devSubApps)
// if (ConfigInfo.mode === 'test') registerMicroApps(proSubApps)
// if (ConfigInfo.mode === 'pro') registerMicroApps(proSubApps)

// registerMicroApps(subApps, lifecycle)

export const initRegisterMicroApps = () => {
  // 注册子应用
  if (ConfigInfo.mode === 'dev') registerMicroApps(devSubApps())
  if (ConfigInfo.mode === 'test') registerMicroApps(proSubApps())
  if (ConfigInfo.mode === 'pro') registerMicroApps(proSubApps())
}

// 初始化qiankun全局状态
// 对actions进行封装
initActions()

// 启动 qiankun 微服务
// start()
